<?php
include("../Action/config.php");
//first, get all of text from cilent.
$data = file_get_contents("php://input");
//second, decode data from client because it is encoded through UTF-8
$urldecode_data = urldecode($data);
//third, decode one more time to json type
$array_data = json_decode($urldecode_data, true);

$child_num = $array_data['child_num'];
// $child_type = $array_data['child_type'];
$app_info_arr = array();
$app_info_arr = $array_data['app_info']; 

foreach ($app_info_arr as $app_info) {
	$app_name = $app_info['app_name'];
	$package_name = $app_info['package_name'];
	// $forbidden = $app_info['forbidden'];

	$sql = "SELECT * FROM APP_INFO 
		WHERE package_name='$package_name' and child_num='$child_num'";
	$result = mysql_query($sql);
	$count = mysql_num_rows($result);
	if ($count > 0) {// 이미 DB에 있는 App Info.
		continue;
	} else {
		$sql = "INSERT INTO APP_INFO (app_name, package_name, child_num) 
			values ('$app_name', '$package_name', '$child_num')";
		$result = mysql_query($sql);
	}
}

// $file = $_SERVER['DOCUMENT_ROOT']
// .'/SBWebServer/ApplistDB/['.$child_num.']_applist.txt'; 
// [child_num]_applist.txt 로 저장한다.

// file_put_contents($file, $app_info_arr);

//convert Array to JSON
$json_app_list = json_encode($app_info_arr);

//finally, save file and send it.
// file_put_contents($file, $json_app_list);

echo $json_app_list;
exit();
?>